home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / Filezilla Server / FileZilla_Server-0_9_41.exe / source / interface / Options.cpp < prev    next >
C/C++ Source or Header  |  2011-11-06  |  9KB  |  348 lines

  1. // FileZilla Server - a Windows ftp server
  2.  
  3. // Copyright (C) 2002-2004 - Tim Kosse <tim.kosse@gmx.de>
  4.  
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9.  
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. // Options.cpp: Implementierungsdatei
  20. //
  21.  
  22. #include "stdafx.h"
  23. #include "options.h"
  24. #include "../version.h"
  25. #include "filezilla server.h"
  26. #include "../tinyxml/tinyxml.h"
  27.  
  28. #if defined(_DEBUG) && !defined(MMGR)
  29. #define new DEBUG_NEW
  30. #undef THIS_FILE
  31. static char THIS_FILE[] = __FILE__;
  32. #endif
  33.  
  34. BOOL COptions::m_bInitialized=FALSE;
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // Dialogfeld COptions 
  38.  
  39. COptions::COptions()
  40. {
  41.     for (int i=0;i<IOPTIONS_NUM;i++)
  42.         m_OptionsCache[i].bCached=FALSE;
  43. }
  44.  
  45. COptions::~COptions()
  46. {
  47. }
  48.  
  49. struct t_Option
  50. {
  51.     TCHAR name[30];
  52.     int nType;
  53. };
  54.  
  55. static const t_Option m_Options[IOPTIONS_NUM]={    _T("Start Minimized"),            1,
  56.                                                 _T("Last Server Address"),        0,
  57.                                                 _T("Last Server Port"),            1,
  58.                                                 _T("Last Server Password"),        0,
  59.                                                 _T("Always use last server"),    1,
  60.                                                 _T("User Sorting"),                1,
  61.                                                 _T("Filename Display"),            1
  62.                                                 };
  63.  
  64. void COptions::SetOption(int nOptionID, __int64 value)
  65. {
  66.     Init();
  67.  
  68.     m_OptionsCache[nOptionID-1].bCached = TRUE;
  69.     m_OptionsCache[nOptionID-1].nType = 1;
  70.     m_OptionsCache[nOptionID-1].value = value;
  71.  
  72.     CString valuestr;
  73.     valuestr.Format( _T("%I64d"), value);
  74.  
  75.     TCHAR buffer[MAX_PATH + 1000]; //Make it large enough
  76.     GetModuleFileName( 0, buffer, MAX_PATH );
  77.     LPTSTR pos=_tcsrchr(buffer, '\\');
  78.     if (pos)
  79.         *++pos=0;
  80.     _tcscat(buffer, _T("FileZilla Server Interface.xml"));
  81.  
  82.     USES_CONVERSION;
  83.     char* bufferA = T2A(buffer);
  84.     if (!bufferA)
  85.         return;
  86.  
  87.     TiXmlDocument document;
  88.     if (!document.LoadFile(bufferA))
  89.         return;
  90.  
  91.     TiXmlElement* pRoot = document.FirstChildElement("FileZillaServer");
  92.     if (!pRoot)
  93.         return;
  94.  
  95.     TiXmlElement* pSettings = pRoot->FirstChildElement("Settings");
  96.     if (!pSettings)
  97.         pSettings = pRoot->LinkEndChild(new TiXmlElement("Settings"))->ToElement();
  98.  
  99.     TiXmlElement* pItem;
  100.     for (pItem = pSettings->FirstChildElement("Item"); pItem; pItem = pItem->NextSiblingElement("Item"))
  101.     {
  102.         const char* pName = pItem->Attribute("name");
  103.         if (!pName)
  104.             continue;
  105.         CString name(pName);
  106.         if (name != m_Options[nOptionID-1].name)
  107.             continue;
  108.  
  109.         break;
  110.     }
  111.  
  112.     if (!pItem)
  113.         pItem = pSettings->LinkEndChild(new TiXmlElement("Item"))->ToElement();
  114.     pItem->Clear();
  115.     pItem->SetAttribute("name", ConvToNetwork(m_Options[nOptionID-1].name));
  116.     pItem->SetAttribute("type", "numeric");
  117.     pItem->LinkEndChild(new TiXmlText(ConvToNetwork(valuestr)));
  118.     
  119.     document.SaveFile(bufferA);
  120. }
  121.  
  122. void COptions::SetOption(int nOptionID, CString value)
  123. {
  124.     Init();
  125.  
  126.     m_OptionsCache[nOptionID-1].bCached = TRUE;
  127.     m_OptionsCache[nOptionID-1].nType = 0;
  128.     m_OptionsCache[nOptionID-1].str = value;
  129.  
  130.     TCHAR buffer[MAX_PATH + 1000]; //Make it large enough
  131.     GetModuleFileName( 0, buffer, MAX_PATH );
  132.     LPTSTR pos=_tcsrchr(buffer, '\\');
  133.     if (pos)
  134.         *++pos=0;
  135.     _tcscat(buffer, _T("FileZilla Server Interface.xml"));
  136.  
  137.     USES_CONVERSION;
  138.     char* bufferA = T2A(buffer);
  139.     if (!bufferA)
  140.         return;
  141.  
  142.     TiXmlDocument document;
  143.     if (!document.LoadFile(bufferA))
  144.         return;
  145.  
  146.     TiXmlElement* pRoot = document.FirstChildElement("FileZillaServer");
  147.     if (!pRoot)
  148.         return;
  149.  
  150.     TiXmlElement* pSettings = pRoot->FirstChildElement("Settings");
  151.     if (!pSettings)
  152.         pSettings = pRoot->LinkEndChild(new TiXmlElement("Settings"))->ToElement();
  153.  
  154.     TiXmlElement* pItem;
  155.     for (pItem = pSettings->FirstChildElement("Item"); pItem; pItem = pItem->NextSiblingElement("Item"))
  156.     {
  157.         const char* pName = pItem->Attribute("name");
  158.         if (!pName)
  159.             continue;
  160.         CString name(pName);
  161.         if (name != m_Options[nOptionID-1].name)
  162.             continue;
  163.  
  164.         break;
  165.     }
  166.  
  167.     if (!pItem)
  168.         pItem = pSettings->LinkEndChild(new TiXmlElement("Item"))->ToElement();
  169.     pItem->Clear();
  170.     pItem->SetAttribute("name", ConvToNetwork(m_Options[nOptionID-1].name));
  171.     pItem->SetAttribute("type", "string");
  172.     pItem->LinkEndChild(new TiXmlText(ConvToNetwork(value)));
  173.     
  174.     document.SaveFile(bufferA);
  175. }
  176.  
  177. CString COptions::GetOption(int nOptionID)
  178. {
  179.     ASSERT(nOptionID>0 && nOptionID<=IOPTIONS_NUM);
  180.     ASSERT(!m_Options[nOptionID-1].nType);
  181.     Init();
  182.     
  183.     if (m_OptionsCache[nOptionID-1].bCached)
  184.         return m_OptionsCache[nOptionID-1].str;
  185.  
  186.     //Verification
  187.     switch (nOptionID)
  188.     {
  189.     case IOPTION_LASTSERVERADDRESS:
  190.         m_OptionsCache[nOptionID-1].str = _T("127.0.0.1");
  191.         break;
  192.     default:
  193.         m_OptionsCache[nOptionID-1].str="";
  194.         break;
  195.     }
  196.     m_OptionsCache[nOptionID-1].bCached=TRUE;
  197.     m_OptionsCache[nOptionID-1].nType=0;
  198.     return m_OptionsCache[nOptionID-1].str;
  199. }
  200.  
  201. __int64 COptions::GetOptionVal(int nOptionID)
  202. {
  203.     ASSERT(nOptionID>0 && nOptionID<=IOPTIONS_NUM);
  204.     ASSERT(m_Options[nOptionID-1].nType == 1);
  205.     Init();
  206.     
  207.     if (m_OptionsCache[nOptionID-1].bCached)
  208.         return m_OptionsCache[nOptionID-1].value;
  209.  
  210.     switch (nOptionID)
  211.     {
  212.     case IOPTION_LASTSERVERPORT:
  213.         m_OptionsCache[nOptionID-1].value=14147;
  214.         break;
  215.     default:
  216.         m_OptionsCache[nOptionID-1].value=0;
  217.     }
  218.     m_OptionsCache[nOptionID-1].bCached=TRUE;
  219.     m_OptionsCache[nOptionID-1].nType=0;
  220.     return m_OptionsCache[nOptionID-1].value;
  221. }
  222.  
  223. void COptions::Init()
  224. {
  225.     if (m_bInitialized)
  226.         return;
  227.     m_bInitialized=TRUE;
  228.     TCHAR buffer[MAX_PATH + 1000]; //Make it large enough
  229.     GetModuleFileName( 0, buffer, MAX_PATH );
  230.     LPTSTR pos=_tcsrchr(buffer, '\\');
  231.     if (pos)
  232.         *++pos=0;
  233.     _tcscat(buffer, _T("FileZilla Server Interface.xml"));
  234.     
  235.     for (int i = 0; i < IOPTIONS_NUM; i++)
  236.         m_OptionsCache[i].bCached = FALSE;
  237.     
  238.     USES_CONVERSION;
  239.     char* bufferA = T2A(buffer);
  240.     if (!bufferA)
  241.         return;
  242.  
  243.     TiXmlDocument document;
  244.  
  245.     CFileStatus status;
  246.     if (!CFile::GetStatus(buffer, status) )
  247.     {
  248.         document.LinkEndChild(new TiXmlElement("FileZillaServer"));
  249.         document.SaveFile(bufferA);
  250.     }
  251.     else if (status.m_attribute&FILE_ATTRIBUTE_DIRECTORY)
  252.         return;
  253.  
  254.     if (!document.LoadFile(bufferA))
  255.         return;
  256.  
  257.     TiXmlElement* pRoot = document.FirstChildElement("FileZillaServer");
  258.     if (!pRoot)
  259.         return;
  260.  
  261.     TiXmlElement* pSettings = pRoot->FirstChildElement("Settings");
  262.     if (!pSettings)
  263.         pSettings = pRoot->LinkEndChild(new TiXmlElement("Settings"))->ToElement();
  264.  
  265.     TiXmlElement* pItem;
  266.     for (pItem = pSettings->FirstChildElement("Item"); pItem; pItem = pItem->NextSiblingElement("Item"))
  267.     {
  268.         const char* pName = pItem->Attribute("name");
  269.         if (!pName)
  270.             continue;
  271.         CString name(pName);
  272.         const char* pType = pItem->Attribute("type");
  273.         if (!pType)
  274.             continue;
  275.         CString type(pType);
  276.         TiXmlNode* textNode = pItem->FirstChild();
  277.         if (!textNode || !textNode->ToText())
  278.             continue;
  279.         CString value = ConvFromNetwork(textNode->Value());
  280.  
  281.         for (int i = 0; i < IOPTIONS_NUM; i++)
  282.         {
  283.             if (name != m_Options[i].name)
  284.                 continue;
  285.  
  286.             if (m_OptionsCache[i].bCached)
  287.             {
  288.                 ::AfxTrace( _T("Item '%s' is already in cache, ignoring item\n"), name);
  289.                 break;
  290.             }
  291.  
  292.             if (type == "numeric")
  293.             {
  294.                 if (m_Options[i].nType != 1)
  295.                 {
  296.                     ::AfxTrace( _T("Type mismatch for option '%s'. Type is %d, should be %d"), name, m_Options[i].nType, 1);
  297.                     break;
  298.                 }
  299.                 m_OptionsCache[i].bCached = TRUE;
  300.                 m_OptionsCache[i].nType = 1;
  301.                 _int64 value64=_ttoi64(value);
  302.  
  303.                 switch (i+1)
  304.                 {
  305.                 case IOPTION_LASTSERVERPORT:
  306.                     if (value64 < 1 || value64 > 65535)
  307.                         value64 = 14147;
  308.                     break;
  309.                 default:
  310.                     break;
  311.                 }
  312.  
  313.                 m_OptionsCache[i].value = value64;
  314.             }
  315.             else
  316.             {
  317.                 if (type != "string")
  318.                     ::AfxTrace( _T("Unknown option type '%s' for item '%s', assuming string\n"), type, name);
  319.                 if (m_Options[i].nType != 0)
  320.                 {
  321.                     ::AfxTrace( _T("Type mismatch for option '%s'. Type is %d, should be %d"), name, m_Options[i].nType, 0);
  322.                     break;
  323.                 }
  324.                 m_OptionsCache[i].bCached = TRUE;
  325.                 m_OptionsCache[i].nType = 0;
  326.  
  327.                 m_OptionsCache[i].str = value;
  328.             }
  329.         }
  330.     }
  331. }
  332.  
  333. bool COptions::IsNumeric(LPCTSTR str)
  334. {
  335.     if (!str)
  336.         return false;
  337.     LPCTSTR p=str;
  338.     while(*p)
  339.     {
  340.         if (*p<'0' || *p>'9')
  341.         {
  342.             return false;
  343.         }
  344.         p++;
  345.     }
  346.     return true;
  347. }
  348.